package com.useeinfo.framework.sugar.data;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: 居泽平  Date: 14/12/10, 09:53
 */
@SuppressWarnings("unused")
public class QueryData<T> {

    private final static Logger logger = LoggerFactory.getLogger(QueryData.class);
    private Integer pageNum;
    private Integer pageSize;
    private Long totalNum;
    private List<T> dataList;

    public Long getTotalNum() {
        return totalNum;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public QueryData(Long totalNum) {
        this.totalNum = totalNum;
    }

    public QueryData(List<T> dataList) {
        this.dataList = dataList;
    }

    public QueryData(List<T> dataList, Integer totalNum) {

        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        if (totalNum == null) {
            totalNum = 0;
        }

        this.totalNum = Long.valueOf(totalNum);
        this.dataList = dataList;
    }

    public QueryData(List<T> dataList, Long totalNum, Integer pageNum, Integer pageSize) {

        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        if (totalNum == null) {
            totalNum = 0L;
        }
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 0;
        }

        this.totalNum = totalNum;
        this.dataList = dataList;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public JSONObject toJSONObject() {
        return toJSONObject("toJSONObject");
    }

    public JSONObject toJSONObject(String methodName) {
        JSONObject result = new JSONObject();
        JSONArray jsonArray = new JSONArray();

        for (T t : this.dataList) {
            try {
                Method method = t.getClass().getDeclaredMethod(methodName);
                jsonArray.add(method.invoke(t));
            } catch (Exception ex) {
                logger.error("遍历Object转换JSONObject异常,异常原因:[{}]", ex.getMessage(), ex);
            }
        }
        return TableResultData.Rsult(totalNum.intValue(), pageNum, pageSize, jsonArray);
    }
    public JSONObject toJQJSONObject(String methodName) {
        JSONObject result = new JSONObject();
        JSONArray jsonArray = new JSONArray();

        for (T t : this.dataList) {
            try {
                Method method = t.getClass().getDeclaredMethod(methodName);
                jsonArray.add(method.invoke(t));
            } catch (Exception ex) {
                logger.error("遍历Object转换JSONObject异常,异常原因:[{}]", ex.getMessage(), ex);
            }
        }
        return TableResultData.jqResult(totalNum.intValue(), pageNum, pageSize, jsonArray);
    }

    public static JSONObject nullReultJson(QueryParam queryParam) {
        Integer pageNum = queryParam.getPageNow() == null ? 0 : queryParam.getPageNow();
        Integer pageSize = queryParam.getPageSize() == null ? 0 : queryParam.getPageSize();
        return TableResultData.Rsult(0, pageNum, pageSize, new JSONArray());
    }
}
